package com.devunion.salon.persistence.dao;

import com.devunion.salon.persistence.Product;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Viktar Liaskovich
 */
public class ProductDao extends BaseDaoObject<Product> {
    public Product getByKey(Serializable key) {
        return (Product) getHibernateTemplate().get(Product.class, key);
    }

    public Set<Product> getProducts(final String username) {
        return (Set<Product>) getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                List<Product> result = session.createQuery("select product from Product product, Login login " +
                        "where login.login = :username and product.location = login.location ").
                        setParameter("username", username, Hibernate.STRING).list();

                return result == null || result.isEmpty() ? new HashSet<Product>() : new HashSet<Product>(result);
            }
        });
    }

    public Product getProductByName(final String product, final String username) {
        return (Product) getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                return session.createQuery("select product from Product product, Login login " +
                        "where login.login = :username and product.location = login.location and product.name = :prodName").
                        setParameter("username", username, Hibernate.STRING).
                        setParameter("prodName", product, Hibernate.STRING)
                        .list().get(0);
            }
        });
    }

    public Set<Product> getProductByCriteria(final String value, final String employeeId) {
        return (Set<Product>) getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session)
                    throws HibernateException, SQLException {
                StringBuilder builder = new StringBuilder("select product from Product product, Employee employee " +
                        "where employee.location = product.location and employee.id = :emplId");

                if (!StringUtils.isBlank(value)) {
                    builder.append(" and product.name like :prodName");
                }
                Query query = session.createQuery(builder.toString()).
                        setParameter("emplId", Long.valueOf(employeeId), Hibernate.LONG);
                if (!StringUtils.isBlank(value)) {
                    query.setParameter("prodName", "%" + value + "%", Hibernate.STRING);
                }
                List<Product> result = query.list();
                return ((result == null) || result.isEmpty())
                        ? new HashSet<Product>() : new HashSet<Product>(result);
            }
        });
    }
}